package com.hienLam.crawler.example;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HTTPClientExample {
    public static String siteName = "www.tinhte.vn";
    public static String url = "http://www.tinhte.vn/";
    public static String OUTPUT_FOLDER = "output";//

    public static void main(String[] args) throws Exception {
        File f = new File(OUTPUT_FOLDER);
        System.out.println(" Write data to folder: " + f.getAbsolutePath());
        try{
            String data1 = getDataUsingJavaNet(url );
            System.out.println(data1.length());
            stringToFile(data1, OUTPUT_FOLDER+"/" +siteName+ ".usingJavaNet.htm");
        }catch (Exception e) {
            e.printStackTrace();
        }
        
        String data2 = getDataUsingHTTPClient(url );
        System.out.println(data2.length());
        stringToFile(data2, OUTPUT_FOLDER+"/" +siteName+ ".usingHttpClient.htm");
    }
    
    public static  String getDataUsingJavaNet(String url) throws Exception {
        String dataContent = null;
        HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.connect();
        
        InputStream is = conn.getInputStream();
        byte[] responseByte = getStreamContentAsBytes(is);
        dataContent = new String(responseByte, "UTF-8");
        return dataContent;
    }
    
    public static  String getDataUsingHTTPClient(String url) {
        String response = null;
        HttpGet request = new HttpGet(url );
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse httpResponse;

        try {
            httpResponse = client.execute(request);
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                byte[] responseByte = getStreamContentAsBytes(instream);
                response = new String(responseByte, "UTF-8");
                instream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            client.getConnectionManager().shutdown();
        }
        return response;
    }
    
    static public byte[] getStreamContentAsBytes(InputStream is) throws Exception {
        BufferedInputStream buffer = new BufferedInputStream(is);    
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data  = new byte[4912];      
        int available = -1;
        while((available = buffer.read(data)) > -1){
          output.write(data, 0, available);
        }   
        return output.toByteArray();
    }
    
    public static void stringToFile(String data, String path) throws Exception {
        File f = new File(path);
        File parentFile = f.getParentFile();
        if (parentFile.exists())
            parentFile.mkdirs();
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(path);
        outputStream.write(data.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}
